package com.yb.web.controller.model;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.domain.PageResult;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.model.domain.ModelTemplateParam;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/model/template_param")
@Api(tags = "模板参数管理")
public class ModelTemplateParamController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(ModelTemplateParamController.class.getName());
	
	private static String MODEL_TEMPLATE_PARAM = "model/template_param/template_param";
	private static String MODEL_TEMPLATE_PARAM_ADD = "model/template_param/template_param_add";
	private static String MODEL_TEMPLATE_PARAM_EDIT = "model/template_param/template_param_edit";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('model:template_param')")
	public String main() {
		return MODEL_TEMPLATE_PARAM;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('model:template_param:add')")
	public String addView() {
		return MODEL_TEMPLATE_PARAM_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('model:template_param:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("modelTemplateParam", modelTemplateParamService.getById(id));
		return MODEL_TEMPLATE_PARAM_EDIT;
	}
	
	/**
	 * 模板参数列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('model:template_param:list')")
	@ApiOperation(value="分页查询", notes="分页查询模板参数信息")
	public @ResponseBody PageResult list(ModelTemplateParam modelTemplateParam) {
		return getPageInfo(modelTemplateParamService.getPageByEntity(modelTemplateParam));
	}
	
	/**
	 * 新增模板参数
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('model:template_param:add')")
	@ApiOperation(value="新增模板参数", notes="新增模板参数信息")
	@Log(label = "新增模板参数", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(ModelTemplateParam modelTemplateParam) {
		return toAjax(modelTemplateParamService.insertParam(modelTemplateParam), "新增模板参数" + "[" + modelTemplateParam.getName() + "]");
	}
	
	/**
	 * 编辑模板参数
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('model:template_param:edit')")
	@ApiOperation(value="编辑模板参数", notes="编辑模板参数信息")
	@Log(label = "编辑模板参数", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(ModelTemplateParam modelTemplateParam) {
		return toAjax(modelTemplateParamService.updateParam(modelTemplateParam), "编辑模板参数" + "[" + modelTemplateParam.getName() + "]");
	}
	
	/**
	 * 删除模板参数
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('model:template_param:delete')")
	@ApiOperation(value="删除模板参数", notes="批量删除模板参数信息")
	@Log(label = "删除模板参数", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> modelTemplateParamNames = modelTemplateParamService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(modelTemplateParamService.deleteParam(StringUtil.str2LongArray(ids)), "批量删除模板参数" + modelTemplateParamNames);
	}
}